package Operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class AddBook implements IOperation{

    public void work(BookList bookList){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书的名称：");
        String name = scanner.nextLine();
        System.out.println("请输入书的作者：");
        String author = scanner.nextLine();
        //判断此书是否已经有了
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getPos(i);
            if(name.equals(book.getName()) && author.equals(book.getAuthor())){
                System.out.println("此书已经存在，添加失败");
                return;
            }
        }
        System.out.println("请输入书的价格：");
        int price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入书的类型");
        String type = scanner.nextLine();

        bookList.books[bookList.getUsedSize()] = new Book(name,author, price, type);
        bookList.setUsedSize(bookList.getUsedSize()+1);
        System.out.println("新增成功！");
    }
}
